package com.taptech.doufu.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.ZoomImageView;
import com.taptech.doufu.util.GlideUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * Created by Administrator on 2017/3/14/014.
 */
public class ShowPicActivity extends DiaobaoBaseActivity {
    private ViewPager mViewPager;
    private List<String> list = null;
    private ImageView[] mImageViews = null;
    private TextView tvCount;
    private TextView tvDownload;
    int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_pic);
        findViewById(R.id.ivBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvCount = (TextView) findViewById(R.id.tvCount);
        tvDownload = (TextView) findViewById(R.id.tvDownload);
        tvDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadImage(list.get(index));
            }
        });
        list =  getIntent().getStringArrayListExtra("pic_path_list");
        index = getIntent().getIntExtra("index",0);
        if (list != null && list.size()>0) {
            mImageViews = new ImageView[list.size()];
        }else{
            mImageViews = new ImageView[1];
        }
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                final ZoomImageView imageView = new ZoomImageView(
                        getApplicationContext());
                if (list != null && list.size() > position) {
                    String path = list.get(position);
                    if (path.startsWith("http")) {
                        GlideUtil.displayImage(WeMediaApplication.getInstance(),imageView,path);
                    } else {
                        File file = new File(path);
                        Glide.with(WeMediaApplication.getInstance())
                                .load(file)
                                .error(R.drawable.picture_failed_img)
                                .into(imageView);
                    }
                    container.addView(imageView);
                }
                return imageView;
            }


            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                if (mImageViews.length>position)
                    container.removeView(mImageViews[position]);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1)
            {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                if (list!=null)
                    return list.size();
                else
                    return 0;
            }
        });
        mViewPager.setCurrentItem(index);
        tvCount.setText(index+1+"/"+list.size());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
                tvCount.setText(position+1+"/"+list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void downloadImage(String url){

            //检查文件是否存在
            //// TODO: 16/11/16 优化根据 md5 值 判断文件
            BaseSubscriber<File> baseSubscriber = new BaseSubscriber<File>(this) {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    if (waitDialog != null && waitDialog.isShowing()) {
                        waitDialog.dismiss();
                    }
                    e.printStackTrace();
                    Toast.makeText(ShowPicActivity.this, "图片下载失败！", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNext(File file) {
                    if (waitDialog != null && waitDialog.isShowing()) {
                        waitDialog.dismiss();
                    }
                    Toast.makeText(ShowPicActivity.this, "图片已保存至相册", Toast.LENGTH_SHORT).show();
                }
            };
            RxJavaHelper.downloadFile(url,baseSubscriber, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile(), System.currentTimeMillis()+".jpg" );
    }


    public static void showPic(Context context,String path,int index) {
        ArrayList<String> pList = new ArrayList<>();
        pList.add(path);
        showPic(context,pList,index);
    }

    public static void showPic(Context context, ArrayList<String> list,int index) {
        Intent intent = new Intent();
        intent.setClass(context, ShowPicActivity.class);
        intent.putExtra("pic_path_list", list);
        intent.putExtra("index", index);
        context.startActivity(intent);
    }
}
